from django.db import models
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from django.conf import settings as django_settings
from django.conf.urls.defaults import *
from django import forms

import datetime
from djfacet.constants import *

from fb_utils.mymodels import TimeStampedHiddenModel
from fb_utils.utils import blank_or_string, preview_string

from picklefield.fields import PickledObjectField




#  ====================================

################ MODELS USED TO CACHE FACET VALUES AND COUNTS

#  ====================================



class CachedFacetQuery(TimeStampedHiddenModel):
	"""	 an abstraction of a FB query run to get the count of available facetValues	 """
	facet = models.CharField(max_length=50, verbose_name="the uniquename of the facet")
	resulttype = models.CharField(max_length=50, verbose_name="the uniquename of the result type")
	tot_ids = models.IntegerField(null=True, blank=True, verbose_name="we store the tot number of resulttype objects") 
	# queryargs = models.ForeignKey('CachedQueryArgs', null=True, blank=True,)
	queryargs = models.TextField(null=True, blank=True, verbose_name="Contains a collation of the queryargs IDs - can become pretty big.. so we couldn't use a BIGINT or similar!")
	
	facetvalues = models.ManyToManyField('CachedFacetValue', null=True, blank=True,)
	
	class Meta:
		pass

# 
# 
# class CachedQueryArgs(TimeStampedHiddenModel):
#	""" an abstraction of the filters used in a FB query (constructed succintly through a deterministic function) """
#	facets = models.CharField(max_length=100, verbose_name="a deterministic collation of the uniquename of the facets involved in the query")
#	values = models.CharField(max_length=100, verbose_name="a deterministic collation of the uniquename of the names of the facetValues involved in the query")



class CachedFacetValue(TimeStampedHiddenModel):
	"""	 abstraction of facet values, together with their count """
	facetvalue = models.CharField(max_length=200, null=True, verbose_name="the name of the value")
	facet = models.CharField(max_length=20, null=True, blank=True, verbose_name="NOT USED ? ? the uniquename of the facet")
	count = models.IntegerField(verbose_name="the count", null=True)




class CachedFacetedManager(TimeStampedHiddenModel):
	"""	 contains the pickled backup of the faceted manager	 """
	manager = PickledObjectField()	# the fields that contains tyhe pickled fm instance value
	
	
